package com.woniuxy.amusementparkos.controller.xxy;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.xxy.Good;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.service.xxy.GoodService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

@Controller
@RequestMapping("/store/good")
public class GoodController {
	@Autowired
	GoodService goodService;
	
	//查询全部
	@RequestMapping("/selectAll")
	@ResponseBody
	public TableDataDto<Good> selectAllStore(HttpSession session) {
		int store_id =  (int) session.getAttribute("store_id");
		try {
			List<Good> goods = goodService.selectAllGoods(store_id);
			long count = goodService.selectAllGoodsCount();
			return new TableDataDto<Good>(0, "查询成功", count, goods);
		} catch (Exception e) {
			return new TableDataDto<Good>(500, e.getMessage(), null, null);
		}
		
	}
	@RequestMapping("/insert")
	@ResponseBody
	public ResponseResult<String> insert(Good good,HttpSession session) {
		int store_id = (int) session.getAttribute("store_id");
		Store_Info store = new Store_Info();
		store.setStore_id(store_id);
		good.setStore(store);
		  try { 
			  goodService.insertGood(good); 
			  return new ResponseResult<String>(0, "添加成功", null); 
			  } catch (Exception e) { 
				  return new ResponseResult<String>(500, e.getMessage(), null); 
			 }
	}
	@RequestMapping("/updateGood")
	@ResponseBody
	public ResponseResult<String> updateGood(Good good) {
		try {
			goodService.updateGood(good);
			return new ResponseResult<String>(0, "修改成功", null);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
	}
	@RequestMapping("/deleteGood")
	@ResponseBody
	public ResponseResult<String> deleteGood(Good good) {
		try {
			goodService.deleteGood(good);
			return new ResponseResult<String>(0, "删除成功", null);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
	}
	//以下为路径跳转
	@RequestMapping("/getGood")
	public String  toGood(){
		return "/xxy/商品信息页";
	}
	
	
}
